package pl.edu.pb.wi.cookbook.service;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Service;

@Service
public class EmailService {
    public static final String URL = "http://kuckbuck2.appspot.com/";
    public static final String SENDER = "feszyn@gmail.com";
    public static final String TITLE = "Weryfikacja konta - Kukbuk";

    public void sendMail(String email, String verificationKey)
            throws UnsupportedEncodingException, MessagingException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(SENDER));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        msg.setSubject(TITLE);
        msg.setText(verificationMessage(verificationKey));
        Transport.send(msg);

    }

    private String verificationMessage(String key) {
        return new StringBuilder("Aby aktywować konto kliknij link: ")
                .append(URL).append("verify/").append(key).toString();
    }
}
